package com.cmsky.open.shop.service.impl;

import java.util.UUID;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

import com.cmsky.open.core.base.BasePager;
import com.cmsky.open.core.util.CookieUtil;
import com.cmsky.open.core.util.JsonUtils;
import com.cmsky.open.shop.manager.ShopInfoManager;
import com.cmsky.open.shop.model.ShopInfo;
import com.cmsky.open.shop.service.ShopLoginService;
import com.cmsky.open.shop.vo.query.ShopInfoQuery;

@Service("shopLoginService")
public class ShopLoginServiceImpl implements ShopLoginService {
	protected static final Logger logger = Logger.getLogger(ShopLoginService.class);
	protected static final String shoperCookieName = "suid";
	private ShopInfoManager shopInfoManager;
	private ShardedJedisPool shardedJedisPool;
	
	public void setShopInfoManager(ShopInfoManager shopInfoManager) {
		this.shopInfoManager = shopInfoManager;
	}

	public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
		this.shardedJedisPool = shardedJedisPool;
	}

	public ShopInfo getLoginedShoper(HttpServletRequest request,
			HttpServletResponse response) {
		ShopInfo shoper = null;
		String suid = null;
		Cookie cookie = CookieUtil.getCookie(request, response, shoperCookieName);
		if(null != cookie){
			suid = cookie.getValue();
		}else{
			suid = request.getParameter(shoperCookieName);
		}
		if(StringUtils.isNotBlank(suid)){
			ShardedJedis jedis =  shardedJedisPool.getResource();
			String beanJson = jedis.get(suid);
			if(null != beanJson){
				shoper = JsonUtils.objectFromJson(beanJson, ShopInfo.class);
			}
		}
		return shoper;
	}

	public ShopInfo lgIn(HttpServletRequest request,
			HttpServletResponse response, String username, String passwd) {
		ShopInfoQuery query = new ShopInfoQuery();
		ShopInfo info = null;
		query.setEmail(username);
		query.setPasswd(passwd);
		BasePager<ShopInfo> pager = this.shopInfoManager.findPage("ShopInfo.findPage", query);
		if(pager.getRows()!=null && pager.getRows().size()==1){
			info = (ShopInfo) pager.getRows().get(0);
			logger.info("<|>lgIn<|>"+info+"<|>success<|>");
			String suid = getSUID();
			String statusCode = shardedJedisPool.getResource().set(suid, JsonUtils.jsonFromObject(info));
			if(null!=statusCode){
				CookieUtil.setCookie(request, response, shoperCookieName, suid, 600);
			}
		}
		return info;
	}

	public void lgOut(HttpServletRequest request, HttpServletResponse response) {
		String suid = null;
		Cookie cookie = CookieUtil.getCookie(request, response, shoperCookieName);
		if(null != cookie){
			suid = cookie.getValue();
		}else{
			suid = request.getParameter(shoperCookieName);
		}
		if(StringUtils.isNotBlank(suid)){
			shardedJedisPool.getResource().del(suid);
		}
	}

	public boolean updatePasswd(Integer shopId, String oldPasswd,
			String newPasswd) {
		boolean result = false;
		if(null!=shopId){
			ShopInfoQuery query = new ShopInfoQuery();
			query.setId(shopId);
			query.setPasswd(oldPasswd);
			BasePager pager = this.shopInfoManager.findPage("ShopInfo.findPage", query);
			if(null!=pager.getRows() && pager.getRows().size()==1){
				query.setPasswd(newPasswd);
				if(0 < this.shopInfoManager.updatePass(query)){
					logger.info("<|>updatePasswd<|>"+shopId+"<|>success<|>");
					result = true;
				}
			}
		}
		return result;
	}

	public boolean isSuper(ShopInfo shoper) {
		return shoper==null?false:(shoper.getEmail().equals("root"));
	}
	
	protected String getSUID(){
		return "S"+UUID.randomUUID();
	}
}
